#pragma warning(disable : 4819)
#include "Game.h"
#include "BeginScene.h"
#include "BeginAnimationScene.h"
#include "MakeMapScene.h"
#include "GameScene.h"
Game::Game()
{
    std::random_device rd;
    gen_ = std::mt19937(rd());
}

Game &Game::GetInstance()
{
    // TODO: 在此处插入 return 语句
    static Game instance;
    return instance;
}

bool Game::Init()
{
    //初始化SDL
    if(SDL_Init(SDL_INIT_EVERYTHING)!= 0)
    {
        SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDL_Init Error: %s", SDL_GetError());
        return false;
    }
    // 初始化SDL_image
    if (IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG) != (IMG_INIT_PNG | IMG_INIT_JPG))
    {
        SDL_LogError(SDL_LOG_CATEGORY_ERROR, "IMG_Init Error: %s", IMG_GetError());
        return false;
    }
    // 初始化SDL_mixer
    if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) != 0)
    {
        SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Mix_OpenAudio Error: %s", Mix_GetError());
        return false;
    }
    // TODO: 初始化SDL_ttf

    // 创建窗口
    SDL_Window *window = SDL_CreateWindow("BattleCity", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, windowWidth_, windowHeight_, SDL_WINDOW_SHOWN);
    if (window == nullptr)
    {
        SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDL_CreateWindow Error: %s", SDL_GetError());
        return false;
    }
    icon_ = IMG_Load("fodder/pic/Icon.png");
    SDL_SetWindowIcon(window,icon_);

    // 创建渲染器
    renderer_ = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    if (renderer_ == nullptr)
    {
        SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDL_Createrenderer_ Error: %s", SDL_GetError());
        return false;
    }

    currentScene_ = std::make_unique<BeginAnimationScene>();
    currentScene_->Init();
    return true;
}

Game::~Game()
{
    Clean();
}

void Game::Run()
{
    uint32_t deltaTime = 0;
    while (isRunning_)
    {
        auto beginTime = SDL_GetTicks();
        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
            if (event.type == SDL_QUIT)
            {
                isRunning_ = false;
                break;
            }
            handleEvent(&event);
        }
        update(deltaTime);
        render();
        auto endTime = SDL_GetTicks();
        auto diff = endTime - beginTime;
        if (diff < 16) // 16是1/60s
        {
            SDL_Delay(16 - diff);
            deltaTime = 16;
        }
        else
        {
            deltaTime = diff;
        }
    }
}

void Game::Clean()
{
    //清理SDL_mixer
    Mix_CloseAudio();
    Mix_Quit();
    // 清理SDL_image
    IMG_Quit();
    // 清理SDL_ttf
    // TODO: 清理SDL_ttf
    // 清理SDL
    SDL_DestroyWindow(window_);
}

SDL_Renderer *Game::GetRenderer() const
{
    return renderer_;
}

int Game::GetWindowWidth() const
{
    return windowWidth_;
}

int Game::GetWindowHeight() const
{
    return windowHeight_;
}



int Game::GetRandom(int min, int max)
{
    std::uniform_int_distribution<int> dis(min, max);
    return dis(gen_);
}


void Game::handleEvent(SDL_Event *event)
{
    currentScene_->HandleEvent(event);
}

void Game::update(float deltaTime)
{
    auto result = currentScene_->Update(deltaTime);
    if (result.isSwitchScene) // 需要更换场景
    {
        switch (result.sceneType)
        {
        case SceneType::SceneBegin:
            currentScene_.reset(new BeginScene());
            break;
        case SceneType::SceneGame:
            currentScene_.reset(new GameScene());
            break;
        case SceneType::SceneMakeMap:
            currentScene_.reset(new MakeMapScene());
            break;
        default:
            break;
        }
        currentScene_->Init();
        currentScene_->InitData(result.data);
    }
}

void Game::render()
{
    currentScene_->Render();
}
